%% Select local grid
idtemp=str2double(getenv('SGE_TASK_ID'));
if idtemp<workers
   u0grid_local=u0grid(jobs*(idtemp-1)+1: idtemp*jobs,:);
   u1grid_local=u1grid(jobs*(idtemp-1)+1: idtemp*jobs,:);
   u2grid_local=u2grid(jobs*(idtemp-1)+1: idtemp*jobs,:);
   u3grid_local=u3grid(jobs*(idtemp-1)+1: idtemp*jobs,:);
   u4grid_local=u4grid(jobs*(idtemp-1)+1: idtemp*jobs,:);
else
   u0grid_local=u0grid(jobs*(idtemp-1)+1: end,:);
   u1grid_local=u1grid(jobs*(idtemp-1)+1: end,:);
   u2grid_local=u2grid(jobs*(idtemp-1)+1: end,:);
   u3grid_local=u3grid(jobs*(idtemp-1)+1: end,:);
   u4grid_local=u4grid(jobs*(idtemp-1)+1: end,:);
end

sg_local=size(u1grid_local,1);


addpath /share/apps/mosek-9.2.19/9.2/toolbox/r2015aom

%% Do the job

IdSetM=cell(n_years,1);
for j=1:n_years
    
load(num2str(all_years(j)));  
u1grid_local=U_Gumbel(x,1)*ones(sg_local,1);
PYX_cond_x=PYX_cond(x,:);
    
[sgreduced, ...
id_Ugridreduced, s_id_gridreduced, sU_id_gridreduced,...
P_col,...
Tcoord_zeros,...
Ugridreduced, n_U_sets, indices_pairs_extended, Tcoord, C, C_sign,...
cr_obsequiv, cr_one, cr_zeros,fill_obsequiv, fill_one, fill_zeros, numeqconstraints, RHS_one, RHS_zeros,...
equalorder, n_vertices,infty_pos]=useful_anyparam(u0grid_local, u1grid_local, u2grid_local, u3grid_local, u4grid_local,sg_local,random_indices_pairs) ;  



IdSetM{j}=OneSide(u0grid_local, u1grid_local, u2grid_local, u3grid_local, u4grid_local, ...
               sgreduced, PYX_cond_x, ...
               id_Ugridreduced, s_id_gridreduced, sU_id_gridreduced,...
               P_col, ...
               Tcoord_zeros,...
               Ugridreduced, n_U_sets, indices_pairs_extended, Tcoord, C, C_sign,...
               cr_obsequiv, cr_one, cr_zeros,fill_obsequiv, fill_one, fill_zeros, numeqconstraints, RHS_one, RHS_zeros, equalorder,n_vertices,infty_pos);  


end
filename = sprintf('IdSetM.%d.mat', ID);
save(filename,'IdSetM')  
